<?php
class App_Page_Form_AdminPageEdit extends Main_Form
{
	/**
	 * @var App_Page_Service_Layout
	 */
	protected $_serviceLayout;
	
	/**
	 * @var App_Page_Service_Page
	 */
	protected $_servicePage;
	
	/**
     * @var array
     */
    protected $_decoratorsForm  = array(
        'PrepareElements',
        array('ViewScript', array('viewScript' => '_form/admin-page-edit.phtml')),
    );
	
	/**
	 * @return void
	 */
	public function init()
	{
		parent::init();
		
		$this->_serviceLayout = new App_Page_Service_Layout();
		$this->_servicePage = new App_Page_Service_Page();
		
		// --- pages --- {{
		$pageList = $this->_servicePage->getAll();
		$pageArr = array();
		foreach ($pageList as $pageItem) {
			$pageArr[$pageItem['id']] = str_repeat(" . ", ($pageItem['level'] - 1)*6) 
									  . '/' 
				                      . $pageItem['name'];
		}
		// --- pages --- }}
		
		$this->setMethod('post');
		$this->setDecorators($this->_decoratorsForm);
		
		$this->addElement('hidden', 'old_name', array(
			'order'	=> 5,
			'decorators' => $this->_decoratorsHidden
		));
		
		$this->addElement('text', 'name', array(
			'label'	=> 'Имя',
			'required' => true,
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(0, 255)),
				array(
					'validator' => 'UniqueItem', 'options' => array (
                    	'App_Page_Service_Page', 'old_name', array('parent_id' => 'parent_id'),
                        'Страница с таким названием на текущем уровне уже существует' )
                )
			),
			'order'	=> 10,
		));
		
		$this->addElement('select', 'layout_id', array(
			'label'	=> 'Шаблон',
			'multioptions' => $this->_serviceLayout->getAllIdAndName(), 
			'order'	=> 20,
		));
		
		$this->addElement('select', 'parent_id', array(
			'label'	=> 'Родитель',
			'multioptions' => $pageArr,
			'order'	=> 30,
		));
		
		$this->addElement('text', 'meta_title', array(
			'label'	=> 'Мета-заголовок',
			'order'	=> 34,
		));
		
		$this->addElement('textarea', 'meta_description', array(
			'label'	=> 'Мета-описание',
			'rows' => 2,
            'cols' => 40,
			'maxlength' => 255,
			'escape' => false,
			'order'	=> 35,
		));
		
		$this->addElement('text', 'meta_keywords', array(
			'label'	=> 'Мета-ключевики',
			'order'	=> 36,
		));
		
		$this->addElement('checkbox', 'f_map', array(
			'label'	=> 'Показывать на карте',
			'order'	=> 40,
		));
		
		$this->addElement('checkbox', 'f_active', array(
			'label'	=> 'Статус',
			'order'	=> 50,
		));
		
		$this->addElement('submit', 'addButton', array(
			'label' => 'Сохранить',
			'order'	=> 100,
		));
	}
	
	/**
	 * @param array $data
     * @return Main_Form
     */
    public function setFormValues($data)
    {
		$data['old_name'] = $data['name'];
    	return parent::setFormValues($data);
    }
    
    /**
     * (non-PHPdoc)
     * @see library/Zend/Zend_Form#isValid()
     */
    public function isValid($data) 
    {
    	$res = parent::isValid($data);
    	
    	
    	if ( !empty($data['name']) && $this->_servicePage->isNameForbidden($data['name'], $data['parent_id'])) {
        	$this->getElement('name')->addError('Недопустимое значение');
        	$res = false;
    	}	
    	
    	return $res;
    }

}